package tankgame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * @author DCSGO
 * @version 1.0
 * 游戏绘图区
 */
public class MyPanel extends JPanel implements KeyListener {
    PlayerTank playerTank = null;

    //1.敌人数量可能很多 应选用集合来存储
    //2.以后会有多线程的需求 故选用 Vector
    private int enemyTanksSize;
    Vector<EnemyTank> EnemyTanks = new Vector<>();

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //游戏绘图区
        g.fillRect(0, 0, 1000, 750);
        //绘制玩家坦克
        drawTank(playerTank.getX(), playerTank.getY(), g, playerTank.getDirection(), 0);
        //绘制敌方坦克
        for (int i = 0; i < enemyTanksSize; i++) {
            EnemyTank enemyTank = EnemyTanks.get(i);
            drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirection(), 1);
        }
    }

    /**
     * @param x         坦克左上角x坐标
     * @param y         坦克左上角y坐标
     * @param g         能用于绘制坦克的图案对象
     * @param direction 坦克的方向(0-上 1-右 2-下 3-左)
     * @param type      坦克类型(0 玩家操控的坦克 1 敌人的坦克)
     */
    public void drawTank(int x, int y, Graphics g, int direction, int type) {
        //设置颜色
        switch (type) {
            case 0://玩家操控的坦克
                g.setColor(Color.cyan);
                break;
            case 1://敌人的坦克
                g.setColor(Color.yellow);
                break;

            default:
                System.out.println("暂时没有该坦克类型");
                break;
        }
        //根据方向绘制坦克
        switch (direction) {
            case 0://上
                g.fill3DRect(x, y, 10, 60, false);//左履带
                g.fill3DRect(x + 30, y, 10, 60, false);//右履带
                g.fill3DRect(x + 10, y + 10, 20, 40, false);//车体
                g.fillOval(x + 10, y + 20, 20, 20);//炮台
                g.drawLine(x + 20, y, x + 20, y + 20);//炮筒
                break;

            case 1://右
                g.fill3DRect(x, y, 60, 10, false);//左履带
                g.fill3DRect(x, y + 30, 60, 10, false);//右履带
                g.fill3DRect(x + 10, y + 10, 40, 20, false);//车体
                g.fillOval(x + 20, y + 10, 20, 20);//炮台
                g.drawLine(x + 30, y + 20, x + 60, y + 20);//炮筒
                break;

            case 2://下
                g.fill3DRect(x, y, 10, 60, false);//左履带
                g.fill3DRect(x + 30, y, 10, 60, false);//右履带
                g.fill3DRect(x + 10, y + 10, 20, 40, false);//车体
                g.fillOval(x + 10, y + 20, 20, 20);//炮台
                g.drawLine(x + 20, y + 60, x + 20, y + 20);//炮筒
                break;

            case 3://左
                g.fill3DRect(x, y, 60, 10, false);//左履带
                g.fill3DRect(x, y + 30, 60, 10, false);//右履带
                g.fill3DRect(x + 10, y + 10, 40, 20, false);//车体
                g.fillOval(x + 20, y + 10, 20, 20);//炮台
                g.drawLine(x + 30, y + 20, x, y + 20);//炮筒
                break;

            default:
                System.out.println("暂时没有该方向");
                break;
        }
    }

    public MyPanel() {
        playerTank = new PlayerTank(100, 100, 5);
        enemyTanksSize = 3;
        for (int i = 0; i < enemyTanksSize; i++) {
            EnemyTank enemyTank = new EnemyTank(100 * (i + 1), 0, 3);
            enemyTank.setDirection(2);
            EnemyTanks.add(enemyTank);
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override //响应键盘操作
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {//w-上
            //向上移动
            playerTank.moveUp();
            //改变方向
            playerTank.setDirection(0);
        } else if (e.getKeyCode() == KeyEvent.VK_D) {//d-右
            playerTank.moveRight();
            playerTank.setDirection(1);
        } else if (e.getKeyCode() == KeyEvent.VK_S) {//s-下
            playerTank.moveDown();
            playerTank.setDirection(2);
        } else if (e.getKeyCode() == KeyEvent.VK_A) {//a-左
            playerTank.moveLeft();
            playerTank.setDirection(3);
        }

        //让面板重绘 刷新
        repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}
